package com.vd.wmt.parse;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;

import com.vd.wmt.model.WageDetail;

public class XlsParse {
	private static Map<String, Integer> fieldPosMap = new HashMap<String, Integer>() {
		private static final long serialVersionUID = 1L;
		{
			put("email", 0);
			put("YearMonth", 1);
			put("name", 2);
			put("basicWage", 3);
			put("basicBonus", 4);
			put("outSubsidy", 5);
			put("lunchSubsidy", 6);
			put("dinnerSubsidy", 7);
			put("trafficSubsidy", 8);
			put("houseSubsidy", 9);
			put("travelSubsidy", 10);
			put("managerSubsidy", 11);
			put("workDays", 12);
			put("OtherSubsidy", 13);
			put("OtherDC", 14);
			put("lateCharge", 15);
			put("personIncomeTax", 16);
			put("socialSecurityFund", 17);
			put("housingFund", 18);
			put("reagWage", 19);
			put("wageInBoc", 20);
			put("wageInOther", 21);
			put("comment", 22);
		}
	};
	private Log log = LogFactory.getLog(XlsParse.class);

	public List<WageDetail> parse(Workbook book, String sheetName) {
		log.info("parse start:sheetName" + sheetName);
		if (sheetName == null || sheetName.isEmpty()) {
			sheetName = "Sheet1";
		}
		Sheet sheet = book.getSheet(sheetName);
		if (sheet == null) {
			sheet = book.getSheetAt(0);
		}
		return parse(sheet);
	}

	/**
	 * @desc 反射设置
	 * @param wd
	 * @param row
	 */
	private void SetFieldValue(WageDetail wd, Row row) {
		log.info("SetFieldValue start....");
		try {
			Class<? extends WageDetail> clz = wd.getClass();
			Field[] flds = clz.getDeclaredFields();
			for (Field fld : flds) {
				String fldName = fld.getName();
				String setMethodName = "set" + fldName.substring(0, 1).toUpperCase() + fldName.substring(1);
				Integer pos = fieldPosMap.get(fldName);
				if (pos == null) {
					continue;
				}
				Object fieldValue = null;
				if (fld.getType().equals(String.class)) {
					fieldValue = row.getCell(pos).getStringCellValue();
				} else if (fld.getType().equals(BigDecimal.class)) {
					fieldValue = new BigDecimal(String.valueOf(row.getCell(pos).getNumericCellValue())).setScale(2,
							BigDecimal.ROUND_HALF_UP);
				}else if (fld.getType().equals(Date.class)){
					fieldValue = row.getCell(pos).getDateCellValue();
				}
				else {
					fieldValue = row.getCell(pos);
				}
				Method setMethod = clz.getMethod(setMethodName, fld.getType());
				setMethod.invoke(wd, fieldValue);
				if(row.getCell(pos).getCellComment()!=null){
					String cmts = row.getCell(pos).getCellComment().getString().toString();
					wd.setComment(wd.getComment()+";"+cmts);
				}
				
			}
		} catch (Exception e) {
			log.error("SetFieldValue Exception", e);
		}
		log.info("SetFieldValue end....");
	}

	public List<WageDetail> parse(Sheet sheet) {
		List<WageDetail> list = new ArrayList<WageDetail>(300);
		int idx = 1;
		while (sheet.getRow(idx) != null && !sheet.getRow(idx).getCell(0).getStringCellValue().isEmpty()) {
			WageDetail wd = new WageDetail();
			wd.setIndex(idx);
			Row row = sheet.getRow(idx);
			SetFieldValue(wd, row);
			list.add(wd);
			idx++;
		}
		return list;
	}
}
